package com.hc.mybatis.generator.plugins;

import org.mybatis.generator.api.*;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.exception.ShellException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.generator.internal.util.StringUtility;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MapperPlugin extends PluginAdapter {

    private static final String DEFAULT_DAO_SUPER_CLASS = "com.dfz.base.BaseMapper";
    private static final String DEFAULT_EXPAND_DAO_SUPER_CLASS = "com.dfz.base.BaseExpandMapper";
    private String daoTargetDir;
    private String daoTargetPackage;
    private String daoSuperClass;
    private String expandDaoTargetPackage;
    private String expandDaoSuperClass;
    private ShellCallback shellCallback = null;

    public MapperPlugin() {
        this.shellCallback = new DefaultShellCallback(false);
    }

    @Override
    public boolean validate(List<String> warnings) {
        this.daoTargetDir = this.properties.getProperty("targetProject");
        boolean valid = StringUtility.stringHasValue(this.daoTargetDir);
        this.daoTargetPackage = this.properties.getProperty("targetPackage");
        boolean valid2 = StringUtility.stringHasValue(this.daoTargetPackage);
        this.daoSuperClass = this.properties.getProperty("daoSuperClass");
        if (!StringUtility.stringHasValue(this.daoSuperClass)) {
            this.daoSuperClass = "com.dfz.base.BaseMapper";
        }

        this.expandDaoTargetPackage = this.properties.getProperty("expandTargetPackage");
        this.expandDaoSuperClass = this.properties.getProperty("expandDaoSuperClass");
        if (!StringUtility.stringHasValue(this.expandDaoSuperClass)) {
            this.expandDaoSuperClass = "com.dfz.base.BaseExpandMapper";
        }

        return valid && valid2;
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        XmlElement select = new XmlElement("select");
        select.addAttribute(new Attribute("id", "selectAll"));
        select.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        select.addAttribute(new Attribute("parameterType", introspectedTable.getBaseRecordType()));
        select.addElement(new TextElement(" select <include refid=\"Base_Column_List\" /> from " + introspectedTable.getFullyQualifiedTableNameAtRuntime()));
        XmlElement parentElement = document.getRootElement();
        parentElement.addElement(select);
        return super.sqlMapDocumentGenerated(document, introspectedTable);
    }

    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        JavaFormatter javaFormatter = this.context.getJavaFormatter();
        List<GeneratedJavaFile> mapperJavaFiles = new ArrayList();
        Iterator var4 = introspectedTable.getGeneratedJavaFiles().iterator();

        while (var4.hasNext()) {
            GeneratedJavaFile javaFile = (GeneratedJavaFile) var4.next();
            introspectedTable.getDAOImplementationType();
            CompilationUnit unit = javaFile.getCompilationUnit();
            FullyQualifiedJavaType baseModelJavaType = unit.getType();
            String shortName = baseModelJavaType.getShortName();
            GeneratedJavaFile mapperJavafile = null;
            Interface mapperInterface;
            FullyQualifiedJavaType daoSuperType;
            if (shortName.endsWith("Mapper")) {
                if (StringUtility.stringHasValue(this.expandDaoTargetPackage)) {
                    mapperInterface = new Interface(this.expandDaoTargetPackage + "." + shortName.replace("Mapper", "ExpandMapper"));
                    mapperInterface.setVisibility(JavaVisibility.PUBLIC);
                    mapperInterface.addJavaDocLine("/**");
                    mapperInterface.addJavaDocLine(" * " + shortName + "扩展");
                    mapperInterface.addJavaDocLine(" */");
                    daoSuperType = new FullyQualifiedJavaType(this.expandDaoSuperClass);
                    mapperInterface.addImportedType(daoSuperType);
                    mapperInterface.addSuperInterface(daoSuperType);
                    mapperJavafile = new GeneratedJavaFile(mapperInterface, this.daoTargetDir, javaFormatter);

                    try {
                        File mapperDir = this.shellCallback.getDirectory(this.daoTargetDir, this.daoTargetPackage);
                        File mapperFile = new File(mapperDir, mapperJavafile.getFileName());
                        if (!mapperFile.exists()) {
                            mapperJavaFiles.add(mapperJavafile);
                        }
                    } catch (ShellException var14) {
                        var14.printStackTrace();
                    }
                }
            } else if (!shortName.endsWith("Example") && !shortName.endsWith("Entity")) {
                mapperInterface = new Interface(this.daoTargetPackage + "." + shortName + "");
                mapperInterface.setVisibility(JavaVisibility.PUBLIC);
                mapperInterface.addJavaDocLine("/**");
                mapperInterface.addJavaDocLine(" * 由MyBatis Generator工具自动生成，请不要手动修改");
                mapperInterface.addJavaDocLine(" */");
                daoSuperType = new FullyQualifiedJavaType(this.daoSuperClass);
                String domainObjectFullName = introspectedTable.getContext().getJavaModelGeneratorConfiguration().getTargetPackage() + "." + introspectedTable.getFullyQualifiedTable().getDomainObjectName();
                FullyQualifiedJavaType domainObjectType = new FullyQualifiedJavaType(domainObjectFullName);
                daoSuperType.addTypeArgument(domainObjectType);
                mapperInterface.addImportedType(domainObjectType);
                mapperInterface.addImportedType(daoSuperType);
                mapperInterface.addSuperInterface(daoSuperType);
                mapperJavafile = new GeneratedJavaFile(mapperInterface, this.daoTargetDir, javaFormatter);
                mapperJavaFiles.add(mapperJavafile);
            }
        }

        return mapperJavaFiles;
    }
}

